<?php
/**
 * Created by PhpStorm.
 * User: WangXiaoHong
 * Date: 15-10-13
 * Time: 上午11:45
 */
include_once("/var/www/html/new/conf/le.je");
use \PFT\Db;
include_once '/var/www/html/new/d/module/common/Db.class.php';
include_once '/var/www/html/new/d/common/func.inc.php';
$dbConf = include_once '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息
$le=new go_sql();
$le->connect();
$s = new SoapClient(null,array(
"location" => "http://localhost/open/openService/pft_insideMX.php",
"uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
);
if(isset($_GET['past'])){
    switch ($_GET['past']) {
        case 'seven':
            $begintime=date("Y-m-d",strtotime("-7 day"));
            $endtime  =date("Y-m-d",strtotime("-1 day"));
            break;
        case 'thirty':
            $begintime=date("Y-m-d",strtotime("-30 day"));
            $endtime  =date("Y-m-d",strtotime("-1 day"));
            break;
        case 'month':
            $begintime=date("Y-m-d",strtotime("- 1 year"));
            $endtime  =date("Y-m-d",strtotime("-1 day"));
            break;
        case 'quarter':
            $threeMonthAgo = mktime(0, 0, 0, date('n', time()) - 3, 1, date('Y', time()));
            $year = date('Y', $threeMonthAgo);
            $month = date('n', $threeMonthAgo);
            $startMonth = intval(($month - 1)/3)*3 + 1; // 上季度开始月份
            $endMonth = $startMonth + 2; // 上季度结束月份
            $begintime=date("Y-m-d",strtotime("$year-$startMonth-1"));
            $endtime  =date("Y-m-d",strtotime("$year-$endMonth-1 + 1 month - 1day"));
            break;
        default:
            # code...
            break;
    }
}
switch($_GET['action']) {
    case '1'://销售月报
        $time=array();
        $time_pr=explode('年',$_GET['time']);$time_vs=explode('年',$_GET['time_vs']);
        $time[] =$time_pr[0];
        $time[] =$time_vs[0];
        $query = array('action'=>'sell','flagt'=>$time[0],'flag'=>$time[1],'list'=>$_GET['list']);
        $tid = curr($query);
        echo json_encode($tid);
    break;

    case '2'://签单月报
        $time=array();
        $time_pr=explode('年',$_GET['time']);$time_vs=explode('年',$_GET['time_vs']);
        $time[] =$time_pr[0];
        $time[] =$time_vs[0];
        foreach($time as $time) {
            for($i=1;$i<=12;$i++) {
                $begintime=$time."-$i-01";
                $endtime=date("Y-m-d",strtotime($begintime .' +1 month -1 day'));
                $sql = "select count(id) as sumid from pft_member  where creattime>'$begintime' and creattime<'$endtime' and salesID!=0";
                $GLOBALS['le']->query($sql);
                while($row = $GLOBALS['le']->fetch_assoc()){
                $r[$time][] = (int)$row['sumid'];
                }
            }
        }
        echo json_encode($r);
    break;

    case '3'://会员月报
        $time=array();
        $time_pr= explode('年',$_GET['time']);$time_vs=explode('年',$_GET['time_vs']);
        $time[] = $time_pr[0];
        $time[] = $time_vs[0];
        if($_GET['list']==0) {//全部
            $where="and dtype in ('0','1')";
        }else if($_GET['list']==1) {//供应商
            $where="and dtype in ('0')";
        }else if($_GET['list']==2) {//分销商
            $where="and dtype in ('1') and account>200000 and account<599999";
        }
        else if($_GET['list']==3) {//散客
            $where="and dtype in ('1') and account>10000000000";
        }
        foreach($time as $time) {
            for($i=1;$i<=12;$i++) {
                $begintime = $time."-$i-01";
                $endtime   = date("Y-m-d",strtotime($begintime .' +1 month -1 day'));
                $sql       = "select count(id) as sumid from pft_member  where creattime>'$begintime' and creattime<'$endtime' $where";
                $GLOBALS['le']->query($sql);
                while($row = $GLOBALS['le']->fetch_assoc()) {
                    $r[$time][] = (int)$row['sumid'];
                }
            }
        }
        echo json_encode($r);
    break;

    case '4'://产品月报
        $time=array();
        $time_pr= explode('年',$_GET['time']);$time_vs=explode('年',$_GET['time_vs']);
        $time[] = $time_pr[0];
        $time[] = $time_vs[0];
        if($_GET['list']==0) {//全部
            $where="and p_type in ('A','B','C','H','G')";
        }else if($_GET['list']==1) {//景区
            $where="and p_type in ('A')";
        }else if($_GET['list']==2) {//演出/轮渡
            $where="and p_type in ('H')";
        }else if($_GET['list']==3) {//餐饮
            $where="and p_type in ('G')";
        }else if($_GET['list']==4) {//酒店
            $where="and p_type in ('C')";
        }else if($_GET['list']==5) {//线路
            $where="and p_type in ('B')";
        }
        foreach($time as $time) {
            for($i=1;$i<=12;$i++) {
                $begintime = $time."-$i-01";
                $endtime   = date("Y-m-d",strtotime($begintime .' +1 month -1 day'));
                $sql       = "select count(id) as sumid from uu_land  where addtime>'$begintime' and addtime<'$endtime' $where";
                $GLOBALS['le']->query($sql);
                while($row = $GLOBALS['le']->fetch_assoc()) {
                    $r[$time][] = (int)$row['sumid'];
                }
            }
        }
    echo json_encode($r);
    break;

    case '5':
        $query = array('action'=>'distribution','list'=>$_GET['list'],'past'=>$_GET['past']);
        $num_money = curr($query);
        if(empty($num_money['qt'])) {
            unset($num_money);
            $num_money['tnums']   = 0;
            $num_money['moneys']  = 0;
            exit('{"status":"failure","msg":"此月份无数据","tnums":0,"moneys":0}');
        }
        if($_REQUEST['list']=="chaorder"){
            $target="ordertel,sum(tnum) as s";
        }else if($_REQUEST['list']=="chamoney"){
            $target="ordertel,sum(tprice) as s";
        }else{
            exit;
        }
        $sql="select $target from uu_ss_order where ordertel='12301' and dtime>'$begintime' and dtime<'$endtime'";
        $GLOBALS['le']->query($sql);
        while($row = $GLOBALS['le']->fetch_assoc()) {
            $num_money[$row['ordertel']]['s']=$row['s'];
        }
//        /*淘宝码商  用内部接口调*/
//        $num=$s->Order_Globle_Search("", "", "", "", "", "", "", "", "", "",
//                                     $begintime, $endtime, "", "淘宝码商", "", 1, "", "", "", "1",
//                                     "0", "100", 2, "0", "", "0", "", "0", "", "",
//                                     "", "1", "0",1,"","");
//                                     $num_array=explode(',',$num);
//                                     if($_GET['list']=="chaorder") {
//                                         $s=$num_array[1];
//                                     }else if($_GET['list']=="chamoney"){
//                                         $s=$num_array[2];
//                                     }else{
//                                         exit;
//                                     }
//                                     $num_money[]=array(
//                                                        'mid'=>'淘宝码商',
//                                                        's'=>$s);
//        $num_money['tnums']  += $num_array[1];
//        $num_money['moneys'] += $num_array[2];
//        $num_money['moneys']  = $num_money['moneys']/100;
    echo json_encode($num_money);
    break;

    case '6':
        $query = array('action'=>'paystatus','starttime'=>$_GET['starttime'],'endtime'=>$_GET['endtime'],'past'=>$_GET['past']);
        $tid = curr($query);
        if(empty($tid['balances'])) exit('{"status":"failure","msg":"此日期无数据"}');
        echo json_encode($tid);
    break;

    case '7':
        /*daction为0增加，为1减少 dtype为7电子凭证费 为8短信息费*/
        $sql_ele = "select dtype,sum(dmoney) as moneys from pft_member_journal  where rectime between '$starttime' and
        '$endtime' and daction=0 and fid=1 and (dtype=7 or dtype=8) group by dtype";
        $GLOBALS['le']->query($sql_ele);
        while($row = $GLOBALS['le']->fetch_assoc()){
            $r[$row['dtype']] = (int)$row['moneys'];
        }
        if(empty($r['7'])) exit('{"status":"failure","msg":"此日期无数据"}');
    echo json_encode($r);
    break;

    case 'hot':
        Db::Conf($dbConf['remote_1']);
        $sql="select tid,sum(tnum) as tnum,sum(torder) as torder,sum(money) as money from pft_order_statistics where
             ddate>=? and ddate<=? group  by tid";
        $stmt =Db::Connect()->prepare($sql);
        $stmt->execute(array($begintime,$endtime));
        while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
            $tid_lid      .= $row['tid'].',';
            $r[$row['tid']]=$row;
        };
        $tid_lid=rtrim($tid_lid,',');
        Db::Conf($dbConf['localhost']);
        $sql_ticket="select landid,id from uu_jq_ticket where id in({$tid_lid})";
        $stmt =Db::Connect()->prepare($sql_ticket);
        $stmt->execute();
        while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
            $landid_lid    .= $row['landid'].',';
            $tic[$row['id']]= $row['landid'];//tid
        };
        $landid_lid=rtrim($landid_lid,',');
        $sql_title="select title,id from uu_land where id in({$landid_lid})";
        $stmt =Db::Connect()->prepare($sql_title);
        $stmt->execute();
        while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
            $res[$row['id']] = $row;//键值landid
        }
        foreach($r as $r_key =>$r_val){
            $return[$res[$tic[$r_key]]['id']]['title']   = $res[$tic[$r_key]]['title'];
            $return[$res[$tic[$r_key]]['id']]['orders'] += $r_val['torder'];//landid
            $return[$res[$tic[$r_key]]['id']]['nums']   += $r_val['tnum'];
            $return[$res[$tic[$r_key]]['id']]['moneys'] += $r_val['money'];
        }
        $n = 0;
        $res_html = '';
        if(count($return)){
            foreach($return as $v){
                $sort[] =  $v['orders'];
            }
            array_multisort($sort,SORT_DESC,$return);
            foreach($return as $k=>$v){
                ++$n;
                if($n>=11)continue;
                $v['moneys'] = chu100($v['moneys']);
                $res_html .= <<<HTML
                    <tr>
                        <td class="tL setAlign">{$v['title']}</td>
                        <td>{$v['orders']}</td>
                        <td>{$v['nums']}</td>
                        <td>{$v['moneys']}</td>
                    </tr>
HTML;
            }
        }
        echo json_encode($res_html) ;
        break;

    case 'map':
        Db::Conf($dbConf['remote_1']);
        $sql = "select code,sum(torder) as torder from pft_order_count where ddate>='$begintime' and ddate<='$endtime' group by code";
        $stmt=Db::Connect()->prepare($sql);
        $stmt->execute();
        while($row= $stmt->fetch(PDO::FETCH_ASSOC)){
            $tmp[$row['code']]=$row['torder'];
        }
        $ntc = include('city.php');
        foreach($tmp as $tmp_key=>$tmp_val){
            if(isset($ntc['city'][$tmp_key])){
                //市
                $return[$tmp_key]['province'] = $ntc['city'][$tmp_key][0];//市名称
                $return[$tmp_key]['torder']   = $tmp[$tmp_key];
                //省
                $return[$ntc['city'][$tmp_key][1]]['province'] = $ntc['province'][$ntc['city'][$tmp_key][1]];
                $return[$ntc['city'][$tmp_key][1]]['torder']  += $tmp[$tmp_key];

            }else{
                unset($tmp[$tmp_key]);
            }
        }
        $max = 0;
        $js = '[';
        if(count($return))
            foreach ($return as $k => $v) {
                if($max<$v['torder']) $max = $v['torder'];
                $js .= '{"name": "'.$v['province'].'","value": '.$v['torder'].'},';
            }
        $max = strlen($max)>2?(substr($max,0,strlen($max)-2)+1).'00':100;
        $js.= '{"max":'.$max.'},';
        $js = rtrim($js,',');
        $js.=']';
        exit($js);
        break;
    default : exit('{"status":"failure","msg":"参数错误"}');
}

function chu100($money){
    if($money>1000000){
        $money = $money/1000000;
        $money = number_format($money,1).'万元';
    }else{
        $money = $money/100;
        $money = number_format($money,2).'元';
    }
    return $money;
}
function curr($query)
{
    $query['time'] = $_SERVER['REQUEST_TIME'];
    $query['token'] = md5($query['time'].'0393156885586815305050516493889730425518949732797083380078766300585');
    $url = "http://s.12301.cc/pft/wxh/c/sell.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1);
//    //    return curl_exec($ch);
//    var_dump(curl_exec($ch));
//        exit;
    return json_decode(curl_exec($ch),true);
}
function an_verify($query)
{
    define('SEC_KEY','7667659191929397103470329788807862402627565924638280479623896');
    return md5($query['btime'].SEC_KEY)==$query['token'];
}
Db::Close();